import { defineStore, storeToRefs } from 'pinia'

import useCounterStore from './counter'

const counterStore = useCounterStore()

const { count } = storeToRefs(counterStore)

const useEmpStore = defineStore('emp', {
  state: () => {
    return {
      /** @type {{ deptno: number, dname: string, loc: string }[]} */
      depts: [
        {
          deptno: 10,
          dname: 'ACCOUNTING',
          loc: 'NEW YORK'
        },
        {
          deptno: 20,
          dname: 'ACCOUNTING',
          loc: 'NEW YORK'
        },
        {
          deptno: 30,
          dname: 'ACCOUNTING',
          loc: 'NEW YORK'
        },
        {
          deptno: 40,
          dname: 'ACCOUNTING',
          loc: 'NEW YORK'
        }
      ],
      /** @type {{ deptno: number, dname: string, loc: string } } */
      dept: {
        deptno: 10,
        dname: 'ACCOUNTING',
        loc: 'NEW YORK'
      },
      /** @type { idnex:number } */
      index: -1
    }
  },
  // 定义计算属性
  getters: {
    // 所有的getter方法都必须以state为形参，默认实参是state
    allDepts(state) {
      return state.depts
    },
    currentindex: (state) => state.index
  },
  actions: {
    addDept() {
      const dept = {
        deptno: 50,
        dname: '东软集团',
        loc: '辽宁沈阳'
      }
      this.depts.push(dept)
      console.log(count)
    }
  }
})

export default useEmpStore
